#coding=utf-8
'''
Created on Jun 1, 2011

@author: Peter Harrington
'''
from numpy import *

def loadDataSet(fileName, delim='\t'):
    fr = open(fileName)
    stringArr = [line.strip().split(delim) for line in fr.readlines()]
    datArr = [map(float,line) for line in stringArr]
    return mat(datArr)

def pca(dataMat, topNfeat=9999999):
    meanVals = mean(dataMat, axis=0)
    meanRemoved = dataMat - meanVals #减去平均值
    covMat = cov(meanRemoved, rowvar=0) #计算协方差
    eigVals,eigVects = linalg.eig(mat(covMat)) #求得特征向量和特征值
    eigValInd = argsort(eigVals)            #从小到大排序.获取序号
    eigValInd = eigValInd[:-(topNfeat+1):-1]  #截取前N个特征值的序号
    redEigVects = eigVects[:,eigValInd]       #获取前N个特征向量，并从大到小排序
    lowDDataMat = meanRemoved * redEigVects  #数据转换到新的空间
    reconMat = (lowDDataMat * redEigVects.T) + meanVals #计算出原始数据
    return lowDDataMat, reconMat

def replaceNanWithMean():
    datMat = loadDataSet('secom.data', ' ')
    numFeat = shape(datMat)[1]
    for i in range(numFeat):
        meanVal = mean(datMat[nonzero(~isnan(datMat[:,i].A))[0],i]) #values that are not NaN (a number)
        datMat[nonzero(isnan(datMat[:,i].A))[0],i] = meanVal  #set NaN values to mean
    return datMat
